package com.zyh.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zyh.entity.*;
import com.zyh.service.IAdminRoleService;
import com.zyh.service.IAdminService;
import com.zyh.service.IResourceService;
import com.zyh.service.IRoleResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.ws.soap.Addressing;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhang
 * @since 2022-05-08
 */
@RestController
@RequestMapping("/user")
public class AdminController {
    @Autowired
    private IAdminService adminService;
    @Autowired
    private IAdminRoleService adminRoleService;
    @Autowired
    private IRoleResourceService roleResourceService;
    @Autowired
    private IResourceService resourceService;
    //用户登录
    @GetMapping("/login/{username}/{pwd}")
    public ResultEntity<Admin> login(@PathVariable("username") String username,@PathVariable("pwd") String pwd){
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username).eq("password",pwd);
        Admin one = adminService.getOne(wrapper);
        if(one!=null){
            return ResultEntity.ok(one);
        }else{
            return ResultEntity.error("用户名或密码错误！");
        }
    }
    //用户列表展示
    @GetMapping("/showAll")
    public ResultEntity<List<Admin>> showAll(){
        return ResultEntity.ok(adminService.list());
    }
    //添加用户
    @PostMapping("/insert")
    public ResultEntity<List<Admin>> insert(@RequestBody Admin admin){
        return adminService.save(admin)==true ? ResultEntity.ok() : ResultEntity.error();
    }
    //删除用户
    @DeleteMapping("/delete/{adminId}")
    public ResultEntity<Admin> delete(@PathVariable("adminId") Integer adminId){
        return adminService.removeById(adminId)==true ? ResultEntity.ok() : ResultEntity.error();
    }
    //根据用户标记查询用户信息
    @GetMapping("/selectById/{adminId}")
    public ResultEntity<Admin> selectById(@PathVariable("adminId") Integer adminId){
        Admin byId = adminService.getById(adminId);
        return byId!=null ? ResultEntity.ok(byId) : ResultEntity.error("此用户不存在！");
    }
    //修改用户信息
    @PutMapping("/update")
    public ResultEntity<Admin> update(@RequestBody Admin admin){
        return adminService.updateById(admin)==true ? ResultEntity.ok() : ResultEntity.error("修改失败！");
    }

    //查看该用户拥有哪些角色
    @GetMapping("/selectRoleId/{userId}")
    public ResultEntity<List<AdminRole>> selectRoleId(@PathVariable("userId") Integer userId){
        return ResultEntity.ok(adminRoleService.list(new QueryWrapper<AdminRole>().eq("admin_id",userId)));
    }

    //查询当前登录人的权限ID
    @GetMapping("/selectResourcesByUserId/{userId}")
    public ResultEntity selectResourcesByUserId(@PathVariable("userId") Integer userId){
        // TODO: 2022/5/10 动态获取用户ID 
        List<Object> roleIds = adminRoleService.listObjs(new QueryWrapper<AdminRole>().select("role_id").eq("admin_id", userId));
        List<Object> resourceIds = roleResourceService.listObjs(new QueryWrapper<RoleResource>().select("resource_id").in("role_id", roleIds));
        List<Resource> resourceList = resourceService.list(new QueryWrapper<Resource>().in("menu_id", resourceIds));
        List<Resource> collect = resourceList.stream()
                .filter((menu) -> menu.getType() == 0)
                .peek((menu)->menu.setResourceList(resourceService.selectChild(menu.getMenuId(),resourceList)))
                .collect(Collectors.toList());
        return ResultEntity.ok(collect);
    }
}

